package jcataloger.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jcataloger.beans.ImageItem;

/**
 * This class creates the directory structure of given directory. It is thread and runs in background.
 * @author sekobey
 *
 */
public class DirectoryTreeCreator extends Thread {

	private Map<Integer,List<ImageItem>> directoryMap = new HashMap<Integer,List<ImageItem>>();
	private String root;
	private int progress, max;

	/**
	 * default constructor
	 * @param path path of the directory
	 * @param minVal shows the minimum value to be used in progress screen.
	 * @param maxVal shows the maximum value to be used in progress screen.
	 */
	public DirectoryTreeCreator( String path, int minVal, int maxVal ){
		root = path;
		progress = minVal;
		max = maxVal;
	}

	public void run() {
		try {
			findDirectoriesWithFiles(root);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			progress = max;
		}
	}

	private void findDirectoriesWithFiles(String path) throws FileNotFoundException{
		if ( progress != max-1 ) {
			progress++;
		}
		File f = new File( path );
		if ( f.exists() ) {
			if ( f.isDirectory() ) {
				String files[] = f.list();
				if ( !path.equalsIgnoreCase( root ) )
					directoryMap.put( f.hashCode(), getImageItemList(path, files));
				else
					// do not put root directory into list. 
					directoryMap.put( 0, getImageItemList(path, files));
				if ( files != null ) {
					for ( String s:files ) {
						File temp = new File( path+File.separator+s);
						if ( temp.isDirectory() )
							findDirectoriesWithFiles( path+File.separator+s );
					}
				}
			}
		}
		else
			throw new FileNotFoundException(path+" is not found");

	}

	private List<ImageItem> getImageItemList( String path, String files[] ) {
		List<ImageItem> result = new ArrayList<ImageItem>();
		if ( files != null ) { 
			for ( String s : files ) {
				File temp = new File(path+File.separator+s);
				File parent = null;
				if ( !path.equalsIgnoreCase( root ) )
					parent = new File( path );

				ImageItem item = new ImageItem(temp, parent != null ? parent : null);
				result.add(item);
			}
		}
		return result;
	}

	/**
	 * Returns the created directory structure.
	 * @return returns the directory structure
	 */
	public Map<Integer, List<ImageItem>> getDirectoryMap() {
		return directoryMap;
	}

	/**
	 * 
	 * @return the maximum value to be used in progress screen
	 */
	public int getMax() {
		return max;
	}

	/**
	 * Shows the progress of the job. It returns a value between min and max value.
	 * @return the progress value
	 */
	public int getProgress() {
		return progress;
	}

	/**
	 * Lists directory structure of the image to the console.
	 *
	 */
	public void listDirectories() {
		for ( Integer i : directoryMap.keySet() ) {
			List<ImageItem> list = directoryMap.get(i);
			Debug.println("Directory:"+i);
			for ( ImageItem item : list ) {
				Debug.println("\t"+item.getName()+"\tFile:"+item.isFile());
			}
		}
	}
}
